//import fs from 'fs';

//fs.readFile('./test.txt', 'utf8', (err, data) => {
//if (err) throw err;
//console.log("Callback 版本:");





//console.log(data);
//console.log("====================================");






//});

//const data = fs.readFileSync('./test.txt', 'utf8');
//console.log("同步版本:");
//console.log(data);



import fs from 'fs/promises';
// readFile() - Promise 版本
fs.readFile('./test.txt', 'utf8')
.then(data => {
console.log("Promise 版本:");
console.log(data);
console.log("====================================");
}).catch(err => {
console.error(err);
});
// readFile() - Async/Await 版本
const readFile = async () => {
    try {
        const data = await fs.readFile('./test.txt','utf8');
        console.log("Async/Await 版本:");
        console.log(data);
        console.log("====================================");
    } catch (err) {
        console.error(err);
    }
}

const writeFile = async () => {
    try {
    await fs.writeFile('./test.txt', '写⼊⽂件内容', 'utf8');
    console.log("写⼊成功");
    } catch (err) {
    console.error(err);
    }
};
// 追加⽂件
const appendFile = async () => {
    try {
    await fs.appendFile('./test.txt', '\\n这是追加⽂件内容', 'utf8');
    console.log("追加成功");
    } catch (err) {
    console.error(err);
    }
    };
writeFile();
appendFile();
readFile();


    writeFile();

readFile();